package com.oa.lms.apps.business.user.impl;

import java.security.MessageDigest;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.mail.HtmlEmail;

import com.oa.lms.apps.business.user.IMailInfoService;
import com.oa.lms.apps.business.user.IPostInfoService;
import com.oa.lms.apps.business.user.IUserInfoService;
import com.oa.lms.apps.business.user.IUsersService;
import com.oa.lms.apps.constants.IDEDStatus;
import com.oa.lms.apps.dao.user.IUsersDao;
import com.oa.lms.apps.entity.user.UserInfoValue;
import com.oa.lms.apps.entity.user.UsersValue;

public class UsersServiceImpl implements IUsersService {
	
	private IUsersDao usersDao;
	
	private IUserInfoService userInfoService;
	
	private IMailInfoService mailInfoService;

	private IPostInfoService postInfoService;
	
	private UsersValue usersValue;
	
	private UserInfoValue userInfoValue;

	@Override
	public Integer add(String userName, String password, Integer infoId) {
		usersValue.setUserName(userName);
		usersValue.setPassword(password);
		usersValue.setUserInfo(userInfoService.queryByInfoId(infoId));
		usersValue.setStatus(IDEDStatus.ENABLE);
		return usersDao.add(usersValue);
	}

	@Override
	public void modify(Integer userId, String userName, String password) {
		usersValue = this.queryByUserId(userId);
		
		usersValue.setUserName(userName);
		usersValue.setPassword(password);
		
		usersDao.modify(usersValue);
	}
	
	@Override
	public void delete(Integer userId) {
		usersValue = this.queryByUserId(userId);
		usersValue.setStatus(IDEDStatus.DELETE);
		usersDao.modify(usersValue);

		
	}

	@Override
	public UsersValue queryByUserId(Integer userId) {
		return usersDao.queryByUserId(userId);
	}

	@Override
	public UsersValue verifyUser(String userName, String password) {
		if(userName != null){
			usersValue.setUserName(userName);
		}
		if(password != null){
			usersValue.setPassword(password);
		}
		for(UsersValue usersValue:usersDao.queryUsers(this.usersValue, null, 1,1))
			return usersValue;
		return null;
	}

	@Override
	public List<UsersValue> queryUsers(String userName, String phone,
			String mailBox, Integer postId, Integer rows, Integer pageNo) {
		if(userName != null){
			usersValue.setUserName(userName);
		}
		if(phone != null){
			userInfoValue.setPhone(phone);
		}
		if(mailBox != null){
			userInfoValue.setMailInfo(mailInfoService.queryByMailbox(mailBox));
		}
		if(postId != null){
			userInfoValue.setPostInfo(postInfoService.queryByPostId(postId));
		}
		return usersDao.queryUsers(usersValue, userInfoValue, rows, pageNo);
	}
	
	@Override
	public Integer getTotalPageNumber(String userName, String phone, String mailBox, Integer postId, Integer rows) {
		if(userName != null){
			usersValue.setUserName(userName);
		}
		if(phone != null){
			userInfoValue.setPhone(phone);
		}
		if(mailBox != null){
			userInfoValue.setMailInfo(mailInfoService.queryByMailbox(mailBox));
		}
		if(postId != null){
			userInfoValue.setPostInfo(postInfoService.queryByPostId(postId));
		}
		return usersDao.getTotalPageNumber(usersValue, userInfoValue, rows);
	}
	
	@Override
	public String verifyMail(Integer userId) throws Exception{
		usersValue = this.queryByUserId(userId);
		String verifyStr = usersValue.getUserInfo().getMailInfo().getMailBox() + "||"+ usersValue.getUserInfo().getMailInfo().getVerifyCode();
		MessageDigest md = MessageDigest.getInstance("MD5");
		md.update(verifyStr.getBytes());
		String verifyMD5Str = Base64.encodeBase64URLSafeString(md.digest());
		return verifyMD5Str;
	}
	
	@Override
	public void sendMail(Integer userId) {
		usersValue = this.queryByUserId(userId);
		HtmlEmail email = new HtmlEmail();

		email.setTLS(true);
		email.setHostName("smtp.126.com");
		email.setAuthentication("service_lms@126.com", "2012@lms");
		try {

			// 发送方
			email.setFrom("service_lms@126.com", "公司请假管理系统邮件组",
					"UTF-8");
			// 标题
			email.setSubject("欢迎您使用公司请假管理系统");
			// 设置字符集
			email.setCharset("UTF-8");

			// 内容
			String verifyStr = usersValue.getUserInfo().getMailInfo().getMailBox() + "||"+ usersValue.getUserInfo().getMailInfo().getVerifyCode();
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(verifyStr.getBytes());
			String verifyMD5Str = Base64.encodeBase64URLSafeString(md.digest());

			// 接收方
			email.addTo(usersValue.getUserInfo().getMailInfo().getMailBox(), usersValue.getUserName(), "UTF-8");

			String template = usersValue.getUserName()+"，您好，<br/> 这是来自一份由公司请假管理系统自动发出的邮箱确认邮件。<br/><br/>"
					+ "请<a href='http://localhost:8080/leavesys/verifyMail.action?userId="
					+ usersValue.getUserId()
					+ "&str="
					+ verifyMD5Str
					+ "'>登陆</a>系统网站进行邮箱确认...(此链接一次有效)";

			email.setHtmlMsg(template);

			// 发送
			email.send();
			
		} catch (Exception e) {
			System.err.println("SendMail Process is exception.");
		}
		
	}
	
	
	//Getter and Setter

	public IUsersDao getUsersDao() {
		return usersDao;
	}

	public void setUsersDao(IUsersDao usersDao) {
		this.usersDao = usersDao;
	}

	public IUserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(IUserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	public IMailInfoService getMailInfoService() {
		return mailInfoService;
	}

	public void setMailInfoService(IMailInfoService mailInfoService) {
		this.mailInfoService = mailInfoService;
	}

	public IPostInfoService getPostInfoService() {
		return postInfoService;
	}

	public void setPostInfoService(IPostInfoService postInfoService) {
		this.postInfoService = postInfoService;
	}

	public UsersValue getUsersValue() {
		return usersValue;
	}

	public void setUsersValue(UsersValue usersValue) {
		this.usersValue = usersValue;
	}

	public UserInfoValue getUserInfoValue() {
		return userInfoValue;
	}

	public void setUserInfoValue(UserInfoValue userInfoValue) {
		this.userInfoValue = userInfoValue;
	}

	
	

}
